package order.service.service;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import order.service.mapper.OrderMapper;
import order.service.model.OrderInfo;
import order.service.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.hypermedia.DiscoveredResource;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    private  List<ServiceInstance> instances;

    AtomicInteger atomicInteger = new AtomicInteger();
    // 轮训模式使用
//    private int size = 0;
//    @PostConstruct
//    public void init() {
//        //根据应⽤名称获取服务列表
//        // 提取出来, 就不每次都获取了, 但是也有bug, 服务列表变更无法感知
//        instances = discoveryClient.getInstances("product-service"); // 服务发现, 获取服务信息
//        this.size = instances.size();
//    }

    // Spring cloud 的 LoadBalancer负载均衡
    public OrderInfo getOrderInfoById(Integer id) {
        OrderInfo orderInfo = orderMapper.getOrderInfoById(id);
        String url = "http://product-service/product/" + orderInfo.getProductId();
        log.info("远程调用 {}", url);
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }



//    // 模拟实现负载均衡, 调用商品系统集群, 轮训模式
//    public OrderInfo getOrderInfoById(Integer id) {
//        OrderInfo orderInfo = orderMapper.getOrderInfoById(id);
//        URI uri = instances.get(atomicInteger.getAndIncrement() % size).getUri(); // 轮训, 每次获取一次
//        String url = uri + "/product/" + orderInfo.getProductId();
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }

//    // 模拟实现负载均衡, 调用商品系统集群, 随机模式
//    public OrderInfo getOrderInfoById(Integer id) {
//        OrderInfo orderInfo = orderMapper.getOrderInfoById(id);
//        List<ServiceInstance> instances = discoveryClient.getInstances("product-service"); // 服务发现, 获取服务信息
//        Random random = new Random(); // 根据长度, 获取随机数
//        URI uri = instances.get(random.nextInt(instances.size())).getUri(); // 随机获取
//        System.out.println("ProductId: " + orderInfo.getProductId());
//        System.out.println("uri: " + uri);
//        String url = uri + "/product/" + orderInfo.getProductId();
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }
    // 服务发现
//    public OrderInfo getOrderInfoById(Integer id) {
//        OrderInfo orderInfo = orderMapper.getOrderInfoById(id);
//        URI uri = discoveryClient.getInstances("product-service").get(0).getUri();
//        System.out.println("ProductId: " + orderInfo.getProductId());
//        System.out.println("uri: " + uri);
//        String url = uri + "/product/" + orderInfo.getProductId();
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }
}
